﻿package game.exchangeddata 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import game.constants.ConstData;
	import game.events.EventData;
	import common.Utility;
	import game.GlobalVariables;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)	 
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class UserInfo extends EventDispatcher
	{
		public var userName:String; 
		public var preUser:String;
		public var sessionId:String;
		public var isMain:String;
		public var roomId:String;
		
		public var balance:Number;
		public var maxBet:Number;
		public var minBet:Number;
		public var commissionRate:Number;
		public var memberLtdSeq:String;
		public var isExpired:Boolean = false;	
		public var getDate:String;
		public var oldPass:String;
		public var newPass:String;
		public var chip:Array;
		
		public function UserInfo(){}
		
		/**
		 * @author Daisy
		 * @param	result
		 */
		public function resultSetSessionToken(result:String):void {		
			var temp:Array = result.split("@");
			this.sessionId = temp[0];			
		}
		
		/**
		 * @author Daisy
		 * @param	result
		 * @usage To get information about member: balance, commissionRate
		 */
		public function resultSetMemberInfo(result:String):void {		
			this.balance = Number(result.substr(ConstData.POS_MEMBERBETINFO_CREDIT, ConstData.LEN_MEMBERBETINFO_CREDIT));	
			
			this.commissionRate = Number(result.substr(ConstData.POS_MEMBERCOMMISSION_RATIO, ConstData.LEN_MEMBERCOMMISSION_RATIO));									
			dispatchEvent(new Event(EventData.UPDATE_BALANCE));
				
		}
		
		/**
		 * @author Daisy
		 * @param	result
		 * @usage To get information about member: isExpired
		 */
		public function resultSetExpired(result:String):void {		
			var isExpired = result.substr(1, result.length);				
			if (isExpired == "true" || isExpired == true) {
				this.isExpired = true;
			}		
		}
		
		/**
		 * @author Daisy
		 * @param	result
		 * @usage To get information about member: maxBet, minBet, memberLtdSeq, chip Betting
		 */
		public function resultSetMemberLimit(result:String):void {	
				
			resultSetLimit(result, ConstData.TOTAL_NUMCHIP - 2);
		}	
		
		public function resultSetMemberLimitSicbo(result:String):void {	
			
			resultSetLimit(result, ConstData.TOTAL_NUMCHIP );
		}	
		
		public function init(pUserName:String, pPreUser:String, pIsMain:String, pRoomId:String):void {
			this.userName = pUserName; 
			this.preUser = pPreUser;
			this.isMain = pIsMain;
			this.roomId = pRoomId;
		}
		
		public function resultCurrencySign(result:String):void {					
			var tmpArr:Array = new Array();
			tmpArr = result.split("*");
			GlobalVariables.currency = tmpArr[tmpArr.length -1];							
			dispatchEvent(new Event(EventData.UPDATE_CURRENCY));						
		}
		
		private function resultSetLimit(result:String, numChip:uint):void {
			
			this.maxBet = Number(result.substr(ConstData.POS_MEMBERLIMIT_UPPER, ConstData.LEN_MEMBERLIMIT_UPPER));
			this.minBet = Number(result.substr(ConstData.POS_MEMBERLIMIT_LOWER, ConstData.LEN_MEMBERLIMIT_LOWER));
		
			this.chip = new Array();
			var totalNumChip:int = parseInt(result.substr(ConstData.POS_NUMCHIP, ConstData.LEN_NUMCHIP));
			var posChip:int = ConstData.LEN_NUMCHIP + totalNumChip * ConstData.LEN_NUMCHIPAMT;
			var chipArr:String = result.substr(ConstData.POS_NUMCHIP, posChip);	
			var currentPos = 1;						
			for (var i:int = 1; i <= numChip; i++) {
					var chipAmount:Number;
					chipAmount = Number(chipArr.substr(currentPos, ConstData.LEN_NUMCHIPAMT));
					this.chip.push(chipAmount);
					currentPos += ConstData.LEN_NUMCHIPAMT;
			}	
			
			Utility.selectionSort(this.chip, this.chip.length);
			//trace("resultSetLimit" + this.chip);
			//Utility.bubbleSort(this.chip, this.chip.length);
			this.memberLtdSeq = result.substr(ConstData.POS_MEMBER_BETLTDSEQ, ConstData.LEN_MEMBER_BETLTDSEQ);					
			dispatchEvent(new EventData(EventData.UPDATE_USERINFO));		
		}
		
		
	}
}